package com.liukai.novel_commons.db;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.liukai.novel_commons.entities.NovelRack;
import com.liukai.novel_commons.entities.UserInfo;
import com.liukai.novel_commons.utils.LogUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class RackService {

    private DBHelper mDBHelper;
    private SQLiteDatabase mReadableDatabase;
    private SQLiteDatabase mWritableDatabase;

    public RackService(Context context) {
        LogUtils.e(getClass(), context.toString());
//        mDBHelper = DBManager.getDBManager(context).getDBHelper();
        mDBHelper = new DBHelper(context);
        mWritableDatabase = mDBHelper.getWritableDatabase();
        mReadableDatabase = mDBHelper.getReadableDatabase();
    }

    public List<NovelRack> findAllRack() {
        List<NovelRack> novelRackList = new ArrayList<>();
        NovelRack novelRack = new NovelRack();
        Cursor cursor = mReadableDatabase.query("rack", null, null,
                null, null, null, null);
        while (cursor.moveToNext()) {
            novelRack = new NovelRack();
//            Date readTime = new Date(read_time);
//            novelRack.setUserId(user_id)
//                    .setNovelId(novel_id)
//                    .setRecentlyChapter(novel_id)
            String user_id = cursor.getString(cursor.getColumnIndex("user_id"));
            long novel_id = cursor.getLong(cursor.getColumnIndex("novel_id"));
            int recently_read = cursor.getInt(cursor.getColumnIndex("recently_read"));
            int is_update = cursor.getInt(cursor.getColumnIndex("is_update"));
            long add_time = cursor.getLong(cursor.getColumnIndex("add_time"));
            long read_time = cursor.getLong(cursor.getColumnIndex("read_time"));

//            Date addTime = new Date(add_time);
        }
        return novelRackList;
    }

    /**
     * 插入一条用户信息
     *
     * @param userInfo
     */
    public void insertUserInfo(UserInfo userInfo) {
        String insert_sql = "insert into user values (?,?,?,?,?,?)";
        mWritableDatabase.execSQL(insert_sql, new Object[]{userInfo.getUserId(),
                userInfo.getUserName(),
                userInfo.getUserPass(),
                userInfo.getRoleId(),
                userInfo.getUserImg(),
                userInfo.getReadTime()});
        mWritableDatabase.close();
    }

    /**
     * 查找用户信息
     *
     * @return
     */
    public List<UserInfo> findUserInfo() {
        List<UserInfo> userInfoList = new ArrayList<>();
        UserInfo userInfo = new UserInfo();
        Cursor cursor = mReadableDatabase.query("user", null, null,
                null, null, null, null);
        while (cursor.moveToNext()) {
            String user_id = cursor.getString(cursor.getColumnIndex("user_id"));
            String user_name = cursor.getString(cursor.getColumnIndex("user_name"));
            String user_pass = cursor.getString(cursor.getColumnIndex("user_pass"));
            int role_id = cursor.getInt(cursor.getColumnIndex("role_id"));
            String user_img = cursor.getString(cursor.getColumnIndex("user_img"));
            int read_time = cursor.getInt(cursor.getColumnIndex("read_time"));
            userInfo = new UserInfo();
            userInfo.setUserId(user_id).
                    setUserName(user_name)
                    .setUserPass(user_pass)
                    .setRoleId(role_id)
                    .setReadTime(read_time)
                    .setUserImg(user_img);
            userInfoList.add(userInfo);
        }
        mReadableDatabase.close();
        return userInfoList;
    }

    /**
     * 删除用户信息
     *
     * @param userId
     * @return
     */
    public int deleteUserInfoById(String userId) {
        int i = mWritableDatabase.delete("user", "user_id=?", new String[]{userId});
        mWritableDatabase.close();
        return i;
    }



}
